package com.gz.purchase.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gz.purchase.R;
import com.gz.purchase.library.pojo.StorageCheckProductInfo;
import com.gz.purchase.util.ToastUtils;

import java.util.List;

/**
 * 存库查看 界面的适配器
 * author wanner1991
 */
public class StorageCheckAdapter extends BaseAdapter {
    private Context mContext;
    private List<StorageCheckProductInfo> mDataList;

    public StorageCheckAdapter(Context mContext, List<StorageCheckProductInfo> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.storage_check_activity_item,null);
            holder = new ViewHolder();
            holder.iv_product_selected = (ImageView) view.findViewById(R.id.iv_product_selected);
            holder.tv_product_name = (TextView) view.findViewById(R.id.tv_product_name);
            holder.tv_product_unit_and_num = (TextView) view.findViewById(R.id.tv_product_unit_and_num);
            holder.tv_product_shop_name = (TextView) view.findViewById(R.id.tv_product_shop_name);

            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
            final StorageCheckProductInfo info = mDataList.get(i);
            // 初始化选中按钮
            if (info.isSelected()){
                holder.iv_product_selected.setBackgroundResource(R.mipmap.ic_shopping_selected);
            }else{
                holder.iv_product_selected.setBackgroundResource(R.mipmap.ic_shopping_unselected);
            }
            holder.tv_product_name.setText(info.getProduct_name());
            holder.tv_product_unit_and_num.setText("1"+info.getProduct_unit()+"×"+info.getProduct_num());
            holder.tv_product_shop_name.setText(info.getShop_name());

            holder.iv_product_selected.setTag(info);
            holder.iv_product_selected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    StorageCheckProductInfo info2 = (StorageCheckProductInfo) view.getTag();
                    if (info2 != null && info.getProduct_id() == info2.getProduct_id()){
                        if (info2.getProduct_num() <= 0){
                            ToastUtils.showToast(mContext,"库存为不足，禁止加入购物车");
                            return;
                        }
                        if (info2.isSelected()){
                            info2.setIsSelected(false);
                            holder.iv_product_selected.setBackgroundResource(R.mipmap.ic_shopping_unselected);
                        }else{
                            info2.setIsSelected(true);
                            holder.iv_product_selected.setBackgroundResource(R.mipmap.ic_shopping_selected);
                        }

                    }
                }
            });

        return view;
    }

    private class ViewHolder{
        ImageView iv_product_selected;
        TextView tv_product_name;
        TextView tv_product_unit_and_num;
        TextView tv_product_shop_name;

    }
}
